<template>
  <div>
    <transition name="snow-dissolve" @after-leave="onTransitionEnd">
      <div v-if="showWelcome" class="welcome-page">
        <div 
          class="background-image" 
          :class="{ show: showImage }"
          :style="{ backgroundImage: splashImg ? `url(${splashImg})` : 'none' }"
        ></div>
        <div class="welcome-content">
          <div class="welcome-logo">Road Trip <span class="welcome-sub">Templates</span></div>
          <h1 class="welcome-title">This is Road Trip</h1>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

// 动态导入 splash 目录下所有图片
const splashImages = import.meta.glob('../assets/splash/*.{jpg,png,jpeg,gif,webp}', { eager: true, import: 'default' })
const splashList = Object.values(splashImages)

const showWelcome = ref(true)
const showImage = ref(false)
const splashImg = ref('')
const router = useRouter()
let transitionTriggered = false

const onTransitionEnd = () => {
  // 确保只触发一次路由跳转
  if (!transitionTriggered) {
    transitionTriggered = true
    router.push('/main') // 修改为跳转到/main路由
  }
}

onMounted(() => {
  // 随机选一张图片
  if (splashList.length > 0) {
    splashImg.value = splashList[Math.floor(Math.random() * splashList.length)]
  }
  
  // 先显示文字，2秒后再显示图片
  setTimeout(() => {
    showImage.value = true
  }, 2000)
  
  // 5秒后开始消失动画
  setTimeout(() => {
    showWelcome.value = false
  }, 5000)
})
</script>

<style scoped>
.welcome-page {
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: #000; /* 黑色背景 */
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  overflow: hidden;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0;
  transition: opacity 1s ease;
}

.background-image.show {
  opacity: 1;
}

.welcome-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: white;
  z-index: 2;
}

.welcome-logo {
  color: #fff;
  font-size: 2rem;
  font-weight: bold;
  letter-spacing: 1px;
  margin-bottom: 30px;
  text-align: left;
}

.welcome-sub {
  font-size: 1rem;
  font-weight: normal;
  opacity: 0.7;
  margin-left: 6px;
}

.welcome-title {
  font-family: 'Pacifico', cursive, 'Arial';
  font-size: 4vw;
  color: #fff;
  margin: 0;
  text-shadow: 0 4px 24px rgba(0,0,0,0.25);
}

/* 雪花消散动画 */
.snow-dissolve-enter-active, .snow-dissolve-leave-active {
  transition: all 3s ease;
  position: absolute;
  inset: 0;
}

.snow-dissolve-leave-to {
  opacity: 0;
  transform: scale(1.2);
  filter: blur(5px);
}

.snow-dissolve-leave-active {
  position: absolute;
  overflow: hidden;
}

.snow-dissolve-leave-active::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(rgba(255, 255, 255, 0.5) 8%, transparent 9%),
    radial-gradient(rgba(255, 255, 255, 0.4) 12%, transparent 13%),
    radial-gradient(rgba(255, 255, 255, 0.3) 15%, transparent 16%),
    radial-gradient(rgba(255, 255, 255, 0.2) 20%, transparent 21%);
  background-size: 40px 40px, 60px 60px, 80px 80px, 100px 100px;
  background-position: 0 0, 30px 30px, 60px 60px, 90px 90px;
  animation: snow-dissolve 3s ease forwards;
  opacity: 0;
  z-index: 100000;
  pointer-events: none;
}

@keyframes snow-dissolve {
  0% {
    opacity: 0;
    transform: scale(1);
  }
  20% {
    opacity: 0.3;
  }
  50% {
    opacity: 0.7;
    transform: scale(1.05);
  }
  100% {
    opacity: 0;
    transform: scale(1.3);
  }
}
</style>